﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;
using SIGEB.Modelo.moduloEstadisticas;

namespace SIGEB.Modelo
{
    public class conjuntoEstadisticas:conjuntoBase
    {
        public conjuntoEstadisticas(datosAplicacion dA)
            : base(dA)
        {
                
        }

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }

        public LinkedList<datoEstadistico> datosPrueba(DateTime inicial, DateTime final, int dias)
        {
            LinkedList<datoEstadistico> nueva = new LinkedList<datoEstadistico>();
            TimeSpan a = final - inicial;
            int lonBloque = a.Days / dias;
            Random r = new Random();
            for (int i = 0; i < lonBloque; i++)
            {
                nueva.AddLast(new datoEstadistico(r.Next(50), DateTime.Today, DateTime.Today));
            }
            return nueva;
        }

        public LinkedList<datoEstadistico> usoBiblioteca(DateTime inicial, DateTime final, int dias, int medida)
        {
            LinkedList<datoEstadistico> nueva = new LinkedList<datoEstadistico>();
            try
            {
                compConexion.Open();
                string sql = "Select * from estadistica_frecuencia_usuario('" + formatDate(inicial) + "', '" + formatDate(final) + "', " + 
                    dias + ", " + medida + ") as (cantidad int8, inicioIntervalo date, finIntervalo date)";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nueva.AddLast(new datoEstadistico(int.Parse(valores[0]), formatDate(valores[1]), formatDate(valores[2])));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener estadistica");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nueva;
        }

        public LinkedList<datoEstadistico> usoBibliotecaGrupos(DateTime inicial, DateTime final, int dias, string idGrupo, int medida)
        {
            LinkedList<datoEstadistico> nueva = new LinkedList<datoEstadistico>();
            try
            {
                compConexion.Open();
                string sql = "Select * from estadistica_frecuencia_grupo('" + formatDate(inicial) + "', '" + formatDate(final) + "', " + dias + ", " + medida + ", '" + idGrupo + "') as (cantidad int8, inicioIntervalo date, finIntervalo date)";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nueva.AddLast(new datoEstadistico(int.Parse(valores[0]), formatDate(valores[1]), formatDate(valores[2])));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener estadistica");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nueva;
        }

        public LinkedList<datoEstadistico> usoBibliotecaUsuario(DateTime inicial, DateTime final, int dias, string idUsuario, int medida)
        {
            LinkedList<datoEstadistico> nueva = new LinkedList<datoEstadistico>();
            try
            {
                compConexion.Open();
                string sql = "Select * from estadistica_frecuencia_usuario_id('" + formatDate(inicial) + "', '" + formatDate(final) + "', " + dias + ", " + medida + ", '" + idUsuario + "') as (cantidad int8, inicioIntervalo date, finIntervalo date)";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nueva.AddLast(new datoEstadistico(int.Parse(valores[0]), formatDate(valores[1]), formatDate(valores[2])));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener estadistica");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nueva; 
        }

        public LinkedList<datoEstadistico> ingresoDocumentos(DateTime inicial, DateTime final, int dias, int medida)
        {
            LinkedList<datoEstadistico> nueva = new LinkedList<datoEstadistico>();
            try
            {
                compConexion.Open();
                string sql = "Select * from estadistica_ingreso_documentos('" + formatDate(inicial) + "', '" + formatDate(final) + "', " + dias + ", " + medida
                    + ") as (cantidad int8, inicioIntervalo date, finIntervalo date)";
                    NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nueva.AddLast(new datoEstadistico(int.Parse(valores[0]), formatDate(valores[1]), formatDate(valores[2])));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener estadistica");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch { }
            }
            return nueva;
        }

    }
}
